<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class GoodVouchers_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $voucher_id
     * @return mixed
     * 查询单条
     */
    public function get_good_vouchers_by_id($voucher_id){

        $this->db->select('*');
        $this->db->from('good_vouchers');
        $this->db->where('voucher_id',$voucher_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_good_vouchers_by($good_id){

        $this->db->select('*');
        $this->db->from('good_vouchers');
        $this->db->where('good_id',$good_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $voucher_id
     * @param $data
     * @return bool
     * 修改商品信息
     */
    public function sava_good_vouchers($voucher_id,$data){

        $this->db->where('voucher_id',$voucher_id);
        $this->db->update('good_vouchers', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商品
     */
    public function add_good_vouchers($data){

        $this->db->insert('good_vouchers',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }
    /**
     * 批量新增
     * [add_good_vouchers_batch description]
     * @param [type] $data [description]
     */
    public function add_good_vouchers_batch($data){

        $this->db->insert_batch('good_vouchers',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商品
     */
    public function delete_good_vouchers_by($where){
        $this->db->where($where)->delete('good_vouchers');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    //查询所有商家优惠券
    public function get_all_free_vouchers(){
        $query = $this->db->select('*')->from('good_vouchers')->get();
        return $query->result_array();
    }
}
